﻿using System;
using System.Collections.Generic;

using System.Text;
using repliback4.Net.ServerSide;
using repliback4.Net.Protocol.Messages;
using repliback4.Net.Identification;
using repliback4.Misc;


using Tools.Debugging;

namespace repliback4.Net.Protocol.InfoQueryHandling.ServerSide {


    public class BindInfoQueryHandler : ServerInfoQueryHandler {

        public BindInfoQueryHandler ( ServerInfoQueryHandlerManager manager )
            : base( manager ) {

        }

        public override void Handle ( QueryFromClient item ) {
            InfoQuery iq = item.Query;
            Bind query = item.Query.Query as Bind;

            try {

                //this.Manager.Server.ResourceNotInConflict( rid );
                Rid rid = item.Sender.ClientInfo.Rid;
                rid.Resource = query.Resource;

                InfoQuery reiq = iq.MakeResponse( item.Sender.writer.Document, InfoQuery.IqTypes.Result );
                reiq.Query = new Bind( item.Sender.writer.Document ) { Identification = rid };
                //result.Identification = rid;


                item.Query = reiq;
                this.Manager.Server.ResponseIQ( item );
            } catch ( Exception e ) {
                Logger.Exception( this, "Handle exception", e );
            }
        }


        public static void Register ( ServerInfoQueryHandlerManager manager ) {
            ServerInfoQueryHandler handler = new BindInfoQueryHandler( manager );
            manager.Add( typeof( Bind ).GetReplibackTagName( ), handler );
        }
    }
}
